package com.ac.input.web.action.sql;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.jdbc.core.JdbcTemplate;

import com.caac.core.web.action.BaseAction;
import com.caac.core.web.form.BaseForm;

public class ExecSqlAction extends BaseAction {
	protected ActionForward doExecute(ActionMapping mapping, BaseForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String strSql = request.getParameter("STRSQL");
		JdbcTemplate jt = this.getJdbcTemplate();
		if(strSql!=null){
			strSql = strSql.trim();			
		}
		if(strSql.length()<10){
			request.setAttribute("TIP","SQL error.");
			return forward(mapping);
		}
		log.info(strSql);
		String tmp =  strSql.substring(0, 10).toLowerCase();
		try{
			if(tmp.startsWith("select")){
				List list = jt.queryForList(strSql);				
				request.setAttribute("DTLIST",list);
			}else if (tmp.startsWith("insert") || tmp.startsWith("update") || tmp.startsWith("delete")){
				int iCount = jt.update(strSql);
				request.setAttribute("TIP","rows "+ iCount );
			}else{
				jt.execute(strSql);
				request.setAttribute("TIP","success." );
			}
		}catch(Exception e){
			request.setAttribute("TIP",e.getMessage());
		}		
		return forward(mapping);
	}
}